<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>维修管理</el-breadcrumb-item>
                <el-breadcrumb-item>维修记录</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="repairFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="repairFormData.carName" placeholder="请输入维修厂名称"></el-input>
                </el-form-item>

                <el-form-item >
                 <el-input v-model="repairFormData.name" placeholder="请输入维修厂名"></el-input>
                </el-form-item>
                <!--                <el-form-item >-->
                <!--                    <el-input v-model="repairFormData.phoneNum" placeholder="请输入店铺电话"></el-input>-->
                <!--                </el-form-item>-->
                <el-form-item >
                    <el-date-picker
                            v-model="repairFormData.startDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择开始日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item >
                    <el-date-picker
                            v-model="repairFormData.endDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择结束日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"  @click="initShopTable">查询</el-button>
                    <el-button type="primary" @click="tianjia">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="tbCar.carName"
                            label="汽车类型"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="tbMaintainShop.name"
                            label="维修场">
                    </el-table-column>
                    <el-table-column
                            prop="amount"
                            label="维修金额(￥)">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="维修时间">
                    </el-table-column>
                    <el-table-column
                            prop="remark"
                            label="备注">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="editShop(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--修改的对话框-->

        <el-dialog
                title="添加修改信息"
                :visible.sync="editDialogVisible"
                width="40%"
                :before-close="qx">
            <!--model:表单数据-->
            <el-form  label-width="100px" :model="repairForm"  :rules="rules"  ref="ruleForm">
<!--                <el-form-item label="车品牌">-->
<!--                    <el-input v-model="repairForm.carId"></el-input>-->
<!--                </el-form-item>-->

                <el-form-item label="车品牌" prop="carId">
                <el-select v-model="repairForm.carId" placeholder="请选择">
                    <el-option
                            v-for="item in options2"
                            :key="item.id"
                            :label="item.carName"
                            :value="item.id">
                    </el-option>
                </el-select>
                </el-form-item>


<!--                <el-form-item label="维修地址">-->
<!--                    <el-input v-model="repairForm.maintainShopId"></el-input>-->
<!--                </el-form-item>-->


                <el-form-item label="维修场" prop="maintainShopId">
                <el-select v-model="repairForm.maintainShopId"  placeholder="请选择">
                 <el-option
                      v-for="item in options"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                  </el-option>
                 </el-select>
                 </el-form-item>
                <el-form-item label="维修金额" prop="amount">
                    <el-input v-model="repairForm.amount"></el-input>
                </el-form-item>
<!--                <el-form-item label="维修时间">-->
<!--                    <el-input v-model="repairForm.addtime"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="备注">
                    <el-input v-model="repairForm.remark"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="qx">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Repair",
        data(){

            // 金额自定义校验
            var price = (rule, value, callback) => {
                const reg = /^\d+.?\d{0,2}$/
                if (!value) {
                    callback(new Error('请输入价格'))
                } else if (!Number(value)) {
                    callback(new Error('请输入数字值'))
                } else {
                    if (reg.test(value)) {
                        callback()
                    } else {
                        callback(new Error('小数点后最多只能输入两位'))
                    }
                }
            }

            return{
                repairFormData:{},
                tableData:[],
                editDialogVisible:false,
                repairForm:{},
                currentPage:1,
                pageSize:5,
                total:0,
                options:[{}],
                options2:[{}],
                value:'',

                ruleForm: {
                    maintainShopId:'',
                    carId:'',
                    amount:'',
                },
                rules: {
                    maintainShopId: [
                        {  required: true, message: '请选择维修厂', trigger: 'change' }
                    ],
                    carId: [
                        {  required: true, message: '请选择汽车类型', trigger: 'change' }
                    ],
                    amount: [
                        {  required: true, message: '请输入维修金额', trigger: 'change' },
                        // { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确额格式,可保留两位小数' },
                        { validator: price, message:"请输入正确额格式,可保留两位小数", trigger: 'blur'}
                    ]
                }

            }
        },
        created() {
            this.initShopTable();
            this.selectAllShops();
            this.selectAllCar();
        },
        methods:{
            //分页查询
            initShopTable(){
                //get方式不支持json数据



                var that=this;
                this.$http.post(`/safeguard/maintainRecord/initMaintainRecordTable/${this.currentPage}/${this.pageSize}`,this.repairFormData).then(function(resp){
                    // console.log(resp);
                    that.tableData=resp.data.result.records;
                    that.total=resp.data.result.total;
                });

            },
            //添加
            tianjia(){
                this.editDialogVisible=true;
                this.repairForm={};
                this.repairForm
            },
            deleteById(id){
                var that=this;

                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete(`/safeguard/maintainRecord/deleteById/${id}`).then(function (resp) {
                        that.$message.success(resp.data.message);
                        that.initShopTable();

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

                this.currentPage=1;
            },
            editShop(id){
                this.editDialogVisible=true;
                var that=this;
                this.$http.get(`/safeguard/maintainRecord/selectById/${id}`).then(function(resp){
                    // console.log(resp)
                    that.repairForm=resp.data;
                })
            },
            submit(){

                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        var that=this;
                        if(this.repairForm.id!=null){
                            //JSON对象
                            //修改提交
                            this.$http.put(`/safeguard/maintainRecord/update`,this.repairForm).then(function(resp){
                                // console.log(resp)
                                if(resp.data.success==true){
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initShopTable();
                                }
                            })
                        }else{
                            //添加提交
                            this.$http.post(`/safeguard/maintainRecord/insertMaintainRecord`,this.repairForm).then(function (resp) {
                                if(resp.data.success==true){
                                    // console.log(resp)
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initShopTable();
                                }
                            })
                        }
                    } else {

                        return false;
                    }
                });


            },

            qx(){
                this.$refs.ruleForm.resetFields();
                this.editDialogVisible=false;
            },



            handleSizeChange(v){
                this.pageSize=v;
                this.initShopTable();
            },
            handleCurrentChange(v){
                this.currentPage=v;
                this.initShopTable();
            },


            // 汽车下拉
            selectAllCar(){
                var that=this;
                this.$http.post(`/safeguard/car/findAllCars`).then(function(resp){
                    // console.log(resp);
                    that.options2=resp.data;

                });
            },

            //维修场下拉
            selectAllShops(){
                var that=this;
                this.$http.post(`/safeguard/maintainShop/findAllShops`).then(function(resp){
                    // console.log(resp);
                    that.options=resp.data;

                });
            }

        },
    }
</script>

<style scoped>

</style>